//
//  HorizontalSlider.m
//  Guazi
//
//  Created by koolearn on 11-4-18.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "HorizontalSlider.h"
#import "NameValue.h"


@implementation HorizontalSlider

@synthesize leftButton;
@synthesize rightButton;
@synthesize title1;
@synthesize title2;
@synthesize boxView;
@synthesize titles;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame delegate:(id)aDelegate titles:(NSArray*)pairs 
{
    self = [super initWithFrame:frame];
    if ( self ) {
		self.delegate = aDelegate;
		self.titles = pairs;
		
		int x = kHorizontalSliderBorder;
		CGRect rect = CGRectMake(x, kHorizontalSliderBorder, 
								 kHorizontalSliderButton, kHorizontalSliderButton );
		self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[leftButton setFrame:rect];
		[leftButton setTitle:@"<" forState:UIControlStateNormal];
		[leftButton addTarget:self action:@selector(slidePrev:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:leftButton];
		
		x = x + kHorizontalSliderButton + kHorizontalSliderBorder;
		rect = CGRectMake(x, kHorizontalSliderBorder, 
								  kHorizontalSliderTotalWidth - 4*kHorizontalSliderBorder - 2*kHorizontalSliderButton, kHorizontalSliderButton - 1);
		self.boxView = [[[UIView alloc] initWithFrame:rect] autorelease];
		boxView.clipsToBounds = YES;
		[self addSubview:boxView];
		
		
		currentPosition = 0;
		NameValue* pair = (NameValue*) [titles objectAtIndex:currentPosition];
		if ( [delegate respondsToSelector:@selector(didChangedTitle:from:)] ) {
			[delegate performSelector:@selector(didChangedTitle:from:) withObject:pair withObject:nil];
		}
		
		rect = CGRectMake( 0, 0, 
						  boxView.bounds.size.width, boxView.bounds.size.height);
		self.title1 = [[[UILabel alloc] initWithFrame:rect] autorelease];
		title1.text = pair.value;
		title1.opaque = NO;
		title1.textAlignment = UITextAlignmentCenter;
		[boxView addSubview:title1];
		
		CGRect rect2 = CGRectMake( 0, boxView.bounds.size.width, 
						  boxView.bounds.size.width, boxView.bounds.size.height);
		self.title2 = [[[UILabel alloc] initWithFrame:rect2] autorelease];
		title2.text = @"";
		title2.textAlignment = UITextAlignmentCenter;
		title2.opaque = NO;
		[boxView addSubview:title2];
		
		x = kHorizontalSliderTotalWidth - kHorizontalSliderBorder - kHorizontalSliderButton;
		rect = CGRectMake(x, kHorizontalSliderBorder, 
						  kHorizontalSliderButton, kHorizontalSliderButton);
		self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[rightButton setFrame:rect];
		[rightButton setTitle:@">" forState:UIControlStateNormal];
		[rightButton addTarget:self action:@selector(slideNext:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:rightButton];
		
		[self setBackgroundColor:[UIColor blackColor]];
		
		currentLabelIndex = 1;
	}
    return self;
}


- (void) slidePrev : (id) sender
{
	UILabel* currentLabel;
	UILabel* nextLabel;
	if ( currentLabelIndex == 1 ) {
		currentLabel = title1;
		nextLabel = title2;
	}
	else {
		currentLabel = title2;
		nextLabel = title1;
	}
	
	
	NameValue* currentPair = (NameValue*) [titles objectAtIndex:currentPosition];

	currentPosition --;
	if ( currentPosition < 0 ) {
		currentPosition = [titles count] - 1;
	}
	
	NameValue* nextPair = (NameValue*) [titles objectAtIndex:currentPosition];
	if ( [delegate respondsToSelector:@selector(didChangedTitle:from:)] ) {
		[delegate performSelector:@selector(didChangedTitle:from:) withObject:nextPair withObject:currentPair];
	}
	
	CGPoint nextCenter = currentLabel.center;
	nextCenter.x = nextCenter.x - nextLabel.frame.size.width;
	nextLabel.center = nextCenter;
	nextLabel.text = nextPair.value;
	
	[UIView beginAnimations:@"groupChanged" context:self];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationDuration:0.5f];
	
	nextLabel.center = currentLabel.center;

	CGPoint currentCenter = currentLabel.center;
	currentCenter.x = currentCenter.x + currentLabel.frame.size.width;
	currentLabel.center = currentCenter;
	
	currentLabelIndex = currentLabelIndex ++;
	if ( currentLabelIndex > 2 ) currentLabelIndex = 1;
	
	[UIView commitAnimations];
	
}


- (void) slideNext : (id) sender
{
	UILabel* currentLabel;
	UILabel* nextLabel;
	if ( currentLabelIndex == 1 ) {
		currentLabel = title1;
		nextLabel = title2;
	}
	else {
		currentLabel = title2;
		nextLabel = title1;
	}
	
	
	NameValue* currentPair = (NameValue*) [titles objectAtIndex:currentPosition];
	
	currentPosition ++;
	if ( currentPosition >= [titles count] ) {
		currentPosition = 0;
	}
	
	NameValue* nextPair = (NameValue*) [titles objectAtIndex:currentPosition];
	if ( [delegate respondsToSelector:@selector(didChangedTitle:from:)] ) {
		[delegate performSelector:@selector(didChangedTitle:from:) withObject:nextPair withObject:currentPair];
	}
	
	CGPoint nextCenter = currentLabel.center;
	nextCenter.x = nextCenter.x + nextLabel.frame.size.width;
	nextLabel.center = nextCenter;
	nextLabel.text = nextPair.value;
	
	[UIView beginAnimations:@"groupChanged" context:self];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationDuration:0.5f];
	
	nextLabel.center = currentLabel.center;
	
	CGPoint currentCenter = currentLabel.center;
	currentCenter.x = currentCenter.x - currentLabel.frame.size.width;
	currentLabel.center = currentCenter;
	
	currentLabelIndex = currentLabelIndex ++;
	if ( currentLabelIndex > 2 ) currentLabelIndex = 1;
	
	[UIView commitAnimations];
	
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {
	[leftButton release];
	[rightButton release];
	[title1 release];
	[title2 release];
	[titles release];
	[boxView release];
    [super dealloc];
}


@end
